﻿using System;
using System.Collections.Generic;
using System.Text;
using IO=System.IO;
using System.IO;

namespace LibUpdate
{
    /// <summary>
    /// 文件型更新操作基类
    /// </summary>
    internal class FileAction : IUpdateAction
    {
        private File _ActionFile;
        private string _VerStr;

        /// <summary>
        /// 升级的文件
        /// </summary>
        public File ActionFile
        {
            get { return _ActionFile; }
            set { _ActionFile = value; }
        }
        private string _AppPath;

        /// <summary>
        /// 程序启动目录
        /// </summary>
        public string AppPath
        {
            get { return _AppPath; }
            set { _AppPath = value; }
        }
        #region IUpdateAction 成员

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        public bool Add()
        {
            try
            {
                IO.FileInfo f = new System.IO.FileInfo(Get_FromFilePath());

                f.CopyTo(Get_ToFilePath());
                return true;
            }
            catch
            {
                return false;
            }
           

        }

        public bool Delete()
        {
            try
            {

                IO.FileInfo f = new System.IO.FileInfo(Get_ToFilePath());
                if (f.Exists)
                {
                    f.Delete();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool Edit()
        {
            try
            {
                //先删除
                Delete();

                string fromFileName = Get_FromFilePath();
                string writename = Get_ToFilePath();
                IO.File.Copy(fromFileName, writename, true);
              
               // IO.FileInfo f = new System.IO.FileInfo();
                 //using (ZipInputStream s = new ZipInputStream(System.IO.File.OpenRead(Get_FromFilePath()))) {

                                          
                 //   ZipEntry theEntry;
                 //   if ((theEntry = s.GetNextEntry()) != null) {
                 //      
                 //       using (FileStream streamWriter = System.IO.File.Create(writename)) {
                 //       int size = 2048;
                 //       byte[] data = new byte[2048];
                 //       while (true) {
                 //           size = s.Read(data, 0, data.Length);
                 //           if (size > 0) {
                 //               streamWriter.Write(data, 0, size);
                 //           }
                 //           else {
                 //               break;
                 //           }
                 //       }
                 //   }
                 //   }
                //  }
               return true;
            }
            catch(Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return false;
            }
        }

        public bool Update()
        {

             //默认使用修改
             return  Edit();
        }

        public bool Exec()
        {
            try
            {
                if(System.IO.File.Exists(Get_FromFilePath()))
                {
                    System.Diagnostics.Process p = new System.Diagnostics.Process();
                    p.StartInfo = new System.Diagnostics.ProcessStartInfo(Get_FromFilePath());
                    p.Start();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

  

        /// <summary>
        /// 当前版本目录
        /// </summary>
        public string VerStrDir
        {
            get { return _VerStr; }
            set { _VerStr = value; }
        }

        /// <summary>
        /// 获得来源路径
        /// </summary>
        protected string  Get_FromFilePath()
        {
         
            return AppPath+Base.UpdatePath  +"\\"+ _VerStr  + _ActionFile.Path + "\\" + _ActionFile.FileName;
        }
        /// <summary>
        /// 获得添加路径
        /// </summary>
        protected string Get_ToFilePath()
        {
            string dir = AppPath + _ActionFile.Path;
           
            if (!System.IO.Directory.Exists(dir))
            {
                System.IO.Directory.CreateDirectory(dir);
            }
            return dir + "\\" + _ActionFile.FileName;
        }
        #endregion

    }
}
